package utils

import (
	"regexp"
)

type StringWrapper struct {
	self string
}

func NewStringWrapper(strs ...string) *StringWrapper {
	w := new(StringWrapper)
	for _, str := range strs {
		w.self += str
	}
	return w
}

func S(strs ...string) *StringWrapper {
	return NewStringWrapper(strs...)
}

func (w *StringWrapper) substitute(tokens map[string]string) string {
	pattern := regexp.MustCompile(`\{(.+?)\}`)

	var replacement string
	tmp := pattern.ReplaceAllStringFunc(w.self, func(matched string) string {
		replacement = tokens[matched]
		if replacement != "" {
			return replacement
		}
		return ""
	})

	return tmp
}

func (w *StringWrapper) FormatByMap(tokens map[string]string) string {
	return w.substitute(tokens)
}
